package com.ziyia.fragmentbestpractice;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ziyia.fragmentbestpractice.pojo.News;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ewsTitleFragment extends Fragment implements AdapterView.OnItemClickListener {
//    private List<News> mlist;
//    private boolean isTwoPane = false;
//
//    @Nullable
//    @org.jetbrains.annotations.Nullable
//    @Override
//    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.news_title_frag, container, false);
//    }
//
//    @Override
//    public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
//        super.onActivityCreated(savedInstanceState);
//
//        getActivity().findViewById(R.id.contentLayout);
//
//        FragmentActivity activity = getActivity();
//
//        if (activity != null) {
//
//            isTwoPane = activity.findViewById(R.id.newsContentLayout) != null;
//
//        }
//
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
//
//        RecyclerView recyclerView = new RecyclerView(getContext());
//
//        recyclerView.setLayoutManager(linearLayoutManager);
//
//        NewsAdapter newsAdapter = new NewsAdapter(getNews());
//
//        recyclerView.setAdapter(newsAdapter);
//    }
//
//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//    }
//
//    class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
//
//        private List<News> mlist;
//
//        public NewsAdapter(List<News> mlist) {
//            this.mlist = mlist;
//        }
//
//        @NonNull
//        @NotNull
//        @Override
//        public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
//            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
//            ViewHolder viewHolder = new ViewHolder(view);
//            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    News news = mlist.get(viewHolder.getAdapterPosition());
//                    String title = news.getTitle();
//                    String content = news.getContent();
//                    if (isTwoPane) {
//                        NewsContentFragment newsContentFragment = new NewsContentFragment();
//                        newsContentFragment.refresh(title, content);
//                    } else {
//                        NewsContentActivity newsContentActivity = new NewsContentActivity();
//                        newsContentActivity.actionStart(parent.getContext(), title, content);
//                    }
//                }
//            });
//
//
//
//            return viewHolder;
//
//        }
//
//        @Override
//        public void onBindViewHolder(@NonNull @NotNull NewsTitleFragment.NewsAdapter.ViewHolder holder, int position) {
//            holder.title.setText(mlist.get(position).getTitle());
//        }
//
//        @Override
//        public int getItemCount() {
//            return mlist.size();
//        }
//
//        public class ViewHolder extends RecyclerView.ViewHolder {
//            private TextView title;
//            public ViewHolder(@NonNull @NotNull View itemView) {
//                super(itemView);
//                title = itemView.findViewById(R.id.newTitle);
//            }
//        }
//    }
//
//
    private List<News> getNews() {
        List<News> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            News news = new News("This is news title " + i, "年少有为：" + i);
            list.add(news);
        }

        return list;
    }

    private ListView newsTitleListView;
    private List<News> newsList;
    private NewsAdapter adapter;
    private boolean isTwoPane;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        newsList = getNews(); // 初始化新闻数据
        adapter = new NewsAdapter(activity, R.layout.news_item, newsList);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) { View view = inflater.inflate(R.layout.news_title_frag, container, false);
        newsTitleListView = (ListView) view.findViewById(R.id.news_title_list_view);
        newsTitleListView.setAdapter(adapter);
        newsTitleListView.setOnItemClickListener(this);
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        if (getActivity().findViewById(R.id.news_content_fragment) != null) {
//            isTwoPane = true; // 可以找到news_content_layout布局时，为双页模式
//        } else {
//            isTwoPane = false; // 找不到news_content_layout布局时，为单页模式
//        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        News news = newsList.get(position);
        if (isTwoPane) {
// 如果是双页模式，则刷新NewsContentFragment中的内容
//            NewsContentFragment newsContentFragment = (NewsContentFragment)
//                    getFragmentManager().findFragmentById(R.layout.newsContentFrag);
//            Fragment fragmentById = getFragmentManager().findFragmentById(R.id.newsContentFrag);
//            NewsContentFragment newsContentFragment = ((NewsContentFragment)fragmentById);
            NewsContentFragment newsContentFragment = new NewsContentFragment();
            newsContentFragment.refresh(news.getTitle(), news.getContent());
        } else {
// 如果是单页模式，则直接启动NewsContentActivity
            NewsContentActivity.actionStart(getActivity(), news.getTitle(),
                    news.getContent());
        }

        Log.d("即将响应的布局", news.toString());
    }


//    private List<News> getNews() {
//        List<News> newsList = new ArrayList<News>();
//        News news1 = new News();
//        news1.setTitle("Succeed in College as a Learning Disabled Student");
//        news1.setContent("College freshmen will soon learn to live with a roommate, adjust to a new social scene and survive less-than-stellar dining hall food. Students with learning disabilities will face these transitions while also grappling with a few more hurdles.");
//        newsList.add(news1);
//        News news2 = new News();
//        news2.setTitle("Google Android exec poached by China's Xiaomi");
//        news2.setContent("China's Xiaomi has poached a key Google executive involved in the tech giant's Android phones, in a move seen as a coup for the rapidly growing Chinese smartphone maker.");
//        newsList.add(news2);
//        return newsList;
//    }

}
